﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GmailMetroNotifier.Client.Common;
using GmailMetroNotifier.Client.Properties;

namespace GmailMetroNotifier.Client.UIP
{
    /// <summary>
    /// Implements an <see cref="INotificationProvider"/> that uses a <see cref="NotifyIcon"/> for notifications.
    /// </summary>
    public class NotifyIconNotificationProvider : INotificationProvider
    {
        private NotifyIcon _notifyIcon = null;

        /// <summary>
        /// Initializes a new instance using the specified <see cref="NotifyIcon"/> for notifications.
        /// </summary>
        /// <param name="notifyIcon">The <see cref="NotifyIcon"/> to be used for notifications.</param>
        public NotifyIconNotificationProvider(NotifyIcon notifyIcon)
        {
            this._notifyIcon = notifyIcon;
        }

        /// <summary>
        /// Shows the <see cref="NotifyIcon"/> balloon.
        /// </summary>
        /// <param name="message">The message to show.</param>
        /// <param name="severity">The severity of the message, which also defines the icon to show in the balloon.</param>
        public void Notify(string message, NotificationSeverity severity)
        {
            this.Notify(message, severity, null);
        }

        /// <summary>
        /// Shows the <see cref="NotifyIcon"/> balloon.
        /// </summary>
        /// <param name="message">The message to show.</param>
        /// <param name="severity">The severity of the message, which also defines the icon to show in the balloon.</param>
        /// <param name="resultHandler">The action to be called for handling the notification result.</param>
        public void Notify(string message, NotificationSeverity severity, Action<NotificationResult> resultHandler)
        {
            ToolTipIcon icon = ConvertSeverityToToolTipIcon(severity);

            this._notifyIcon.ShowBalloonTip(500, Resources.DESKTOP_MSGBOX_DEFAULT_TITLE, message, icon);

            if (resultHandler != null)
            {
                resultHandler(NotificationResult.None);
            }
        }

        private static ToolTipIcon ConvertSeverityToToolTipIcon(NotificationSeverity severity)
        {
            ToolTipIcon toolTipIcon = ToolTipIcon.None;

            switch (severity)
            {
                case NotificationSeverity.Information:
                    toolTipIcon = ToolTipIcon.Info;
                    break;

                case NotificationSeverity.Warning:
                    toolTipIcon = ToolTipIcon.Warning;
                    break;

                case NotificationSeverity.Error:
                    toolTipIcon = ToolTipIcon.Error;
                    break;
            }

            return toolTipIcon;
        }
    }
}
